﻿using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.Model;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelWordToHtml
{
    public static class ExcelToHtmlUtils
    {
        private static string GetColor(HSSFColor color)
        {
            StringBuilder builder = new StringBuilder(7);
            builder.Append('#');
            foreach (byte num2 in color.RGB)
            {
                builder.Append(((byte)num2).ToString("x2"));
            }
            return builder.ToString();
        }

        private static string GetColor(XSSFColor color)
        {
            StringBuilder builder = new StringBuilder(7);
            builder.Append('#');
            foreach (byte num2 in color.RGB)
            {
                builder.Append(((byte)num2).ToString("x2"));
            }
            return builder.ToString();
        }

        public static string GetHexBgColor(this IWorkbook workbook, ICellStyle cellStyle)
        {
            string str2;
            string str = "";
            if (workbook == null)
            {
                str2 = str;
            }
            else
            {
                if (workbook is HSSFWorkbook)
                {
                    HSSFPalette customPalette = ((HSSFWorkbook)workbook).GetCustomPalette();
                    if (cellStyle.FillPattern != FillPattern.NoFill)
                    {
                        if (cellStyle.FillPattern == FillPattern.SolidForeground)
                        {
                            HSSFColor color = customPalette.GetColor(cellStyle.FillForegroundColor);
                            if (color != null)
                            {
                                str = GetColor(color);
                            }
                        }
                        else
                        {
                            HSSFColor color = customPalette.GetColor(cellStyle.FillBackgroundColor);
                            if (color != null)
                            {
                                str = GetColor(color);
                            }
                        }
                    }
                }
                else if (cellStyle.FillPattern != FillPattern.NoFill)
                {
                    if (cellStyle.FillPattern == FillPattern.SolidForeground)
                    {
                        IndexedColors colors = null;
                        try
                        {
                            colors = IndexedColors.ValueOf(cellStyle.FillForegroundColor);
                        }
                        catch (Exception)
                        {
                        }
                        string hexString = null;
                        if (colors != null)
                        {
                            hexString = colors.HexString;
                        }
                        else
                        {
                            XSSFColor fillForegroundColorColor = (XSSFColor)cellStyle.FillForegroundColorColor;
                            if (fillForegroundColorColor != null)
                            {
                                hexString = GetColor(fillForegroundColorColor);
                            }
                        }
                        if (hexString != null)
                        {
                            str = hexString;
                        }
                    }
                    else
                    {
                        IndexedColors colors2 = null;
                        try
                        {
                            colors2 = IndexedColors.ValueOf(cellStyle.FillBackgroundColor);
                        }
                        catch (Exception)
                        {
                        }
                        string hexString = null;
                        if (colors2 != null)
                        {
                            hexString = colors2.HexString;
                        }
                        else
                        {
                            XSSFColor fillBackgroundColorColor = (XSSFColor)cellStyle.FillBackgroundColorColor;
                            if (fillBackgroundColorColor != null)
                            {
                                hexString = GetColor(fillBackgroundColorColor);
                            }
                        }
                        if (hexString != null)
                        {
                            str = hexString;
                        }
                    }
                }
                str2 = str;
            }
            return str2;
        }

        public static string GetHexColor(this IWorkbook workbook, short colorNum)
        {
            string str2;
            string hexString = "";
            if (workbook == null)
            {
                str2 = hexString;
            }
            else
            {
                if (workbook is HSSFWorkbook)
                {
                    HSSFColor color = ((HSSFWorkbook)workbook).GetCustomPalette().GetColor(colorNum);
                    if (color != null)
                    {
                        hexString = GetColor(color);
                    }
                }
                else
                {
                    IndexedColors black = null;
                    try
                    {
                        black = IndexedColors.ValueOf(colorNum);
                    }
                    catch (Exception)
                    {
                        black = IndexedColors.Black;
                    }
                    if (black != null)
                    {
                        hexString = black.HexString;
                    }
                    else
                    {
                        XSSFColor themeColor = ((XSSFWorkbook)workbook).GetStylesSource().GetTheme().GetThemeColor(colorNum);
                        if (themeColor != null)
                        {
                            hexString = GetColor(themeColor);
                        }
                    }
                }
                str2 = hexString;
            }
            return str2;
        }

        public static string GetHexFontColor(this IWorkbook workbook, IFont font)
        {
            string str = "";
            if (workbook is HSSFWorkbook)
            {
                HSSFColor color = ((HSSFWorkbook)workbook).GetCustomPalette().GetColor(font.Color);
                if (color != null)
                {
                    str = GetColor(color);
                }
            }
            else
            {
                IndexedColors black = null;
                try
                {
                    black = IndexedColors.ValueOf(font.Color);
                }
                catch (Exception)
                {
                    black = IndexedColors.Black;
                }
                string hexString = null;
                if (black != null)
                {
                    hexString = black.HexString;
                }
                else
                {
                    StylesTable stylesSource = ((XSSFWorkbook)workbook).GetStylesSource();
                    XSSFColor color = ((stylesSource == null) || (stylesSource.GetTheme() == null)) ? ((XSSFFont)font).GetXSSFColor() : stylesSource.GetTheme().GetThemeColor(font.Color);
                    if (color != null)
                    {
                        hexString = GetColor(color);
                    }
                }
                if (hexString != null)
                {
                    str = hexString;
                }
            }
            return str;
        }
    }
}
